<?php
/* 	
	PHP 5 Script Filename: mysqladapter.class.php
	Author: Andrey Sumtsov
	Wrote in: Saint-Petersburg
	Wrote Date: 03.05.2007
	Version: 1.0
*/

class MysqlAdapter {
	var $connetion, $query, $result, $table;
	function __construct() {
		$this->setConnection();
	}
	function setConnection() {
		// --> secure
		if (!defined('CFG_FILE')) define('CFG_FILE','E:/Sites/scan/connection.ini');
		file_exists(CFG_FILE) or die('Configuration file is not exists.');
		$config = parse_ini_file(CFG_FILE);
			$host = @$config['host'] or die('<i>Host</i> variable is not defined in configuration file.');
			$user = @$config['user'] or die('<i>User</i> variable is not defined in configuration file.');
			$pass = @$config['password'] or die('<i>Password</i> variable is not defined in configuration file.');
		 	$dbase = @$config['db'] or die('<i>Db</i> variable is not defined in configuration file.');
		// <--
		$this->connection = mysql_connect($host,$user,$pass);
		if (!$this->connection) die('Establishing connection was failed. Please, check user name, password and host name.');
		@mysql_select_db($dbase) or die('Specified database is not exist.');
		$this->setQuery('set names cp1251');
	}
	function getTable ($qstr) {
		if ($qstr == '') die('Empty query string.');
		if (!mysql_ping($this->connection)) $this->setConnection();
		/* Performing SQL query */
		$this->result = mysql_query($qstr) or die('Query failed: '.mysql_error());
		/* Preparing data-array */
		$this->table = array();
		while ($row = mysql_fetch_assoc($this->result)) {
			$this->table[] = $row;
		}
		/* Return the data */
		return $this->table;
	}
	function setQuery($qstr) {
		if ($qstr == '') die('Empty query string.');
		if (!mysql_ping($this->connection)) $this->setConnection();
		$this->result = mysql_query($qstr);
		if ($this->result) return false; else return true;
	}
	function __destruct() {
		@mysql_close($this->connection);
	}
}